# -*- coding: utf-8 -*-

import base64
from wiget2 import Ui_Form
from PyQt5 import QtCore, QtGui, QtWidgets

def get_message():
    s = input()
    return s

def get_key():
    key = input()
    if key == '':
        key = 'none_public_key'
    return key

def init_box(key):
    """
    S盒
    """
    s_box = list(range(256))#256个字节
    j = 0
    for i in range(256):
        j = (j + s_box[i] + ord(key[i % len(key)])) % 256
        s_box[i], s_box[j] = s_box[j], s_box[i]
    return s_box

def ex_encrypt1(plain,box):
    #加密输出
    res=[]
    i=j=0
    for s in plain:
        i = (i + 1) % 256
        j = (j + box[i]) % 256
        box[i], box[j] = box[j], box[i]
        t = (box[i] + box[j]) % 256
        k = box[t]
        res.append(chr(ord(s) ^ k))
    cipher = "".join(res)
    return cipher

def ex_encrypt2(plain,box):
    #加密输出
    res=[]
    i=j=0
    for s in plain:
        i = (i + 1) % 256
        j = (j + box[i]) % 256
        box[i], box[j] = box[j], box[i]
        t = (box[i] + box[j]) % 256
        k = box[t]
        res.append(chr(ord(s) ^ k))
    cipher = "".join(res)
    plain=plain
    return cipher


def getmode1(msg,key1):

    message = msg
    key = key1
    box = init_box(key)
    cipher= ex_encrypt1(message, box)
    return cipher

def getmode2(msg,key1):

    message = msg
    key = key1
    box = init_box(key)
    cipher= ex_encrypt2(message, box)
    return cipher

